package com.chengv.redismq.pojo;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * MQ 消息实体
 *
 * @author chengv
 */
@Data
public class MQMessage {

    /**
     * 消息 ID
     */
    public static final String ID = "id";

    /**
     * 消息负载
     */
    private Object payload;

    /**
     * 消息头
     */
    private Map<String, Object> headers;

    public MQMessage() {
    }

    public MQMessage(Object payload) {
        this(payload, null);
    }

    public MQMessage(Object payload, Map<String, Object> headers) {
        Assert.notNull(payload, "payload 不能为空");

        this.payload = payload;

        this.headers = headers != null ? new HashMap<>(headers) : new HashMap<>();
        if (this.headers.get(ID) == null) {
            this.headers.put(ID, IdUtil.getSnowflake().nextIdStr());
        }
    }
}